package com.gemo.demo.mvc;

import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvc implements WebMvcConfigurer {
    /**
     *
     * @param registry
     * registry给指定的视图添加控制层
     * login
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //http://localhost:8088/ProductWeb/
        registry.addViewController("/").setViewName("login");
        registry.addViewController("/toLogin").setViewName("login");
    }
    //注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new WebInterceptor()).addPathPatterns("/**")
        .excludePathPatterns("/css/**")
        .excludePathPatterns("/js/**")
        .excludePathPatterns("/image/**");
    }
    //配置监听器
    @Bean
    public ServletListenerRegistrationBean listenerRegistrationBean(){
        ServletListenerRegistrationBean listenerBean=new ServletListenerRegistrationBean();
        listenerBean.setListener(new WebListener());
        return  listenerBean;
    }
}
